package org.jeecg.modules.sxaq.receiver;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.boot.starter.rabbitmq.core.BaseRabbiMqHandler;
import org.jeecg.common.annotation.RabbitComponent;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.base.BaseMap;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.modules.sxaq.ior.service.IRecordInoutService;
import org.jeecg.modules.sxaq.utils.AesUtil;
import org.jeecg.modules.sxaq.utils.RabbitMQConstant;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @Description: 二维码处理
 * @Author: DHC
 * @Date:  2020-12-01
 */
@Slf4j
@RabbitComponent(value = "localGunListener")
public class QrcodeRabbitMqListener extends BaseRabbiMqHandler<BaseMap> {

    @Autowired
    private IRecordInoutService recordInoutService;

    @RabbitListener(concurrency = "2-10",bindings = {@QueueBinding(value = @Queue(value = RabbitMQConstant.LOCAL_GUN_QUEUE + "${sxaq.factoryCode}", durable = "true", autoDelete = "false"),
            exchange = @Exchange(value = RabbitMQConstant.LOCAL_GUN_EXCHANGE, type = "topic"),
            key = RabbitMQConstant.LOCAL_GUN_KEY + "${sxaq.factoryCode}")})
    public void onMessage(String message) {

        log.info("-------收到来自云端的模拟扫码请求-------");
        Result result = null;
        try {
            result = decryptCodeScanningGunResult(message);
            JSONObject jsonObject = (JSONObject) result.getResult();
            jsonObject.put("scanName", "手机模拟");
            jsonObject.put("scanCode", "手机模拟");
            recordInoutService.entranceAndExit2(jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
            log.info("-------收到来自云端的模拟扫码请求-------" + e);
        }

    }

    // 解密扫码枪返回数据
    private Result<JSONObject> decryptCodeScanningGunResult(String encryptStr) throws Exception {

        Result<JSONObject> result = new Result();
        result.setCode(CommonConstant.SC_INTERNAL_SERVER_ERROR_500);
        result.setResult(null);
        result.setSuccess(false);

        try {
            if (encryptStr==null || encryptStr.isEmpty()) {
                result.setMessage("信息获取失败");
                return result;
            }

            String aesDecryptString = AesUtil.aesDecrypt(encryptStr,CommonConstant.SXAQ_AES_DECRYPT_KEY);
            if (aesDecryptString==null || aesDecryptString.isEmpty()) {
                result.setMessage("解密失败！");
                return result;
            }

            JSONObject jsonObject = JSONObject.parseObject(aesDecryptString);
            if (jsonObject==null || jsonObject.isEmpty()) {
                result.setMessage("解密失败！");
                return result;
            }

            result.setResult(jsonObject);
        } catch (Exception e) {

        }

        return result;
    }
}
